# coding=utf8
from django.db import models
from django.db.models import permalink
from django.contrib.auth.models import User
from django.contrib import admin

VIEWABLE_STATUS = [3, 4]

class ViewableManager(models.Manager):
    def get_query_set(self):
        default_queryset = super(ViewableManager, self).get_query_set()
        return default_queryset.filter(status_in=VIEWABLE_STATUS)

class Category(models.Model):
    label = models.CharField(blank=True, max_length=50)
    slug = models.SlugField()

    class Meta:
        verbose_name_plural = "类别"
    
    def __unicode__(self):
        return self.label

class Story(models.Model):
    STATUS_CHOICES = (
        (1, "已提交"),
        (2, "已审核"),
        (3, "已发布"),
        (4, "已删除"),
    )

    title = models.CharField(max_length=200)
    slug = models.SlugField()
    category = models.ForeignKey(Category)
    markdown_content = models.TextField()
    html_content = models.TextField(editable=False)
    owner = models.ForeignKey(User)
    status = models.IntegerField(choices=STATUS_CHOICES, default=1)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['modified']
        verbose_name_plural = "文章"

    admin_objects = models.Manager()
    objects = ViewableManager()

    @permalink
    def get_absolute_url(self):
        return("cms-story", (), {'slug': self.slug})

admin.site.register(Category)
admin.site.register(Story)
